package com.newhero.product.module.sys.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.mybatis.mapper.Wrapper;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.common.vo.ZTreeModel;
import com.newhero.product.module.sys.model.App;
import com.newhero.product.module.sys.service.IAppService;

/**
 * @ClassName: AppController
 * @Description: 应用管理控制类
 * @author Caolj 648198687@qq.com
 * @date 2017-3-30 上午9:31:23
 */
@Controller
@RequestMapping("/admin")
public class AppController extends BaseController {

	@Autowired
	private IAppService appServiceImpl;

	@RequiresPermissions("/admin/application")
	@RequestMapping(value = "/application", method = RequestMethod.GET)
	public String application() {
		return "sys/app/application";
	}

	@RequestMapping(value = "/applicationTree", method = RequestMethod.POST)
	@ResponseBody
	public List<ZTreeModel> applicationTree(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		EntityWrapper<App> wrapper = new EntityWrapper<App>();
		wrapper.orderBy("appOrder");
		List<App> sysAppList = appServiceImpl.selectList(wrapper);
		List<ZTreeModel> treeList = new ArrayList<ZTreeModel>();
		for (int i = 0; i < sysAppList.size(); i++) {
			App app = sysAppList.get(i);
			if ("00".equals(app.getAppID()) || "00".equals(app.getAppPID())) {
				ZTreeModel atm = new ZTreeModel(app.getAppID(),
						app.getAppPID(), app.getAppName(), true,
						app.getAppIcon());
				atm.setIcon(request.getContextPath()
						+ "/static/zTree_v3/css/ext_icons/application/application_side_boxes.png");
				treeList.add(atm);
			} else {
				String appName = app.getAppName();
				ZTreeModel atm = new ZTreeModel(app.getAppID(),
						app.getAppPID(), appName, true, app.getAppIcon());
				atm.setIcon(request.getContextPath()
						+ "/static/zTree_v3/css/ext_icons/application/application_view_list.png");
				treeList.add(atm);
			}

		}
		return treeList;
	}

	@RequestMapping(value = "/appForm", method = RequestMethod.GET)
	public String appForm(
			@RequestParam(value = "appID", defaultValue = "", required = false) String appID,
			@RequestParam(value = "appPID", defaultValue = "", required = false) String appPID,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		boolean hasChildren = false;
		if (!appID.equals("")) {
			App sysApp = appServiceImpl.selectById(appID);
			Wrapper<App> wrapper = new EntityWrapper<App>();
			wrapper.eq("appPID", appID);
			int count = appServiceImpl.selectCount(wrapper);
			if (count > 0) {
				hasChildren = true;
			}
			model.addAttribute("appData", sysApp);
			String apppID = sysApp.getAppPID();
			if(StringUtils.isNotBlank(apppID)){
				App parentApp = appServiceImpl.selectById(apppID);
				model.addAttribute("parentAppID", apppID);
				if(parentApp!=null&&!"".equals(parentApp.getAppName())){
					model.addAttribute("parentAppName", parentApp.getAppName());
				}
			}else{
				model.addAttribute("parentAppID", "");
				model.addAttribute("parentAppName", "");
				
			}
			
		} else {
			Map<String, String> emptyApp = new HashMap<String, String>();
			model.addAttribute("appData", emptyApp);
			model.addAttribute("parentAppID", appPID);
			App parentApp = appServiceImpl.selectById(appPID);
			if(parentApp==null){
				model.addAttribute("parentAppName", "");
			}else{
				model.addAttribute("parentAppName", parentApp.getAppName());
			}
		}
		
		model.addAttribute("hasChildren", hasChildren);
		model.addAttribute("appID", appID);
		return "sys/app/appForm";
	}
	
	@ResponseBody
	@RequestMapping(value = "/applicationDelete", method = RequestMethod.POST)
	public Object applicationDelete(
			@RequestParam(value = "appID", required = true) String appID,
			HttpServletRequest request, HttpServletResponse response) {
		appServiceImpl.deleteById(appID);
		return renderSuccess();
	}
	
	@ResponseBody
	@RequestMapping(value = "/deleteAppCascade", method = RequestMethod.POST)
    public Object deleteAppCascade(@RequestParam(value = "appID", required = true) String appID,
        HttpServletRequest request, HttpServletResponse response) {
		List<String> idList = appServiceImpl.getIDListByAppID(appID);
		appServiceImpl.deleteBatchIds(idList);
		return renderSuccess();
    }

	@ResponseBody
	@RequestMapping(value = "/applicationSave", method = RequestMethod.POST)
	public Object applicationSave(
			App sysApp,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			appServiceImpl.insertOrUpdate(sysApp);
			return renderSuccess();
		} catch (Exception e) {
			logger.error(e);
			return renderError("插入或更新失败！");
		}
	}
}
